<?php

use Phalcon\Mvc\User\Component;

/**
 * Elements
 *
 * Helps to build UI elements for the application
 */
class Elements extends Component
{

    private $_headerMenu = array(
        'navbar-left' => array(
            /*'index' => array(
                'caption' => '首页',
                'action' => 'index'
            ),*/
            'index' => array(
                'caption' => 'TDXK',
                'action' => 'index'
            ),
            'businesssystems' => array(
                'caption' => '业务系统',
                'action' => 'search'
            ),
            'serverusers' => array(
                'caption' => '平台用户',
                'action' => 'search'
            ),
            'businessusers' => array(
                'caption' => '业务系统用户',
                'action' => 'userEdit'
            ),
            'portal' => array(
                'caption' => '门户',
                'action' => 'index'
            ),
        ),
        'navbar-right' => array(
            'login' => array(
                'caption' => '登录',
                'action' => 'index'
            ),
        )
    );

    private $_tabs = array(
        'businessSystems' => array(
            'search' => array(
                'caption' => '系统列表',
                'controller' => 'businesssystems',
                'action' => 'search',
                'any' => true
            ),
            'userNew' => array(
                'caption' => '添加系统',
                'controller' => 'businesssystems',
                'action' => 'new',
                'any' => true
            )
        ),
        'serverUsers' => array(
            'search' => array(
                'caption' => '用户列表',
                'controller' => 'serverusers',
                'action' => 'search',
                'any' => true
            ),
            'userNew' => array(
                'caption' => '添加用户',
                'controller' => 'serverusers',
                'action' => 'new',
                'any' => true
            )
        ),
        'businessUsers' => array(
            'search' => array(
                'caption' => '用户列表',
                'controller' => 'businessusers',
                'action' => 'userEdit',
                'any' => true
            ),
            'userNew' => array(
                'caption' => '添加用户',
                'controller' => 'businessusers',
                'action' => 'userNew',
                'any' => true
            )
        )
    );

    /**
     * Builds header menu with left and right items
     *
     * @return string
     */
    public function getMenu()
    {

        $auth = $this->session->get('auth');
        if ($auth) {
            $this->_headerMenu['navbar-right']['login'] = array(
                'caption' => '注销',
                'action' => 'end'
            );
        } else {
            unset($this->_headerMenu['navbar-left']['invoices']);
        }

        $controllerName = $this->view->getControllerName();
        foreach ($this->_headerMenu as $position => $menu) {
            echo '<div class="nav-collapse">';
            /**
             *  IE8不支持Bootsrap3.0.1
             */
            if($position == "navbar-left")
                echo '<ul class="nav navbar-nav ', $position, '" style="float:left" >';
            else
                echo '<ul class="nav navbar-nav ', $position, '" style="float:right" >';
            foreach ($menu as $controller => $option) {
                if ($controllerName == $controller) {
                    echo '<li class="active" style="float:left">';
                } else {
                    echo '<li style="float:left">';
                }
                echo $this->tag->linkTo($controller . '/' . $option['action'], $option['caption']);
                echo '</li>';
            }
            echo '</ul>';
            echo '</div>';
        }

    }

    /**
     * Returns menu tabs
     */
    public function getTabs($menu)
    {
        $controllerName = $this->view->getControllerName();
        $actionName = $this->view->getActionName();
        echo '<ul class="nav nav-tabs">';
        foreach ($this->_tabs[$menu] as $option) {
            if ($option['controller'] == $controllerName && $option['action'] == $actionName ) {
                echo '<li class="active">';
            } else {
                echo '<li>';
            }
            echo $this->tag->linkTo($option['controller'] . '/' . $option['action'], $option['caption']), '</li>';
        }
        echo '</ul>';
    }
}
